#!/usr/bin/env python

# TODO
# Test registration
# Add ability to add multiple notifications when registering, or add additional notifications later?

import sys
sys.path.append('/usr/local/lib/python2.4/site-packages')
from netgrowl import *

from optparse import OptionParser

# Options meant to mimic those of gNotify (http://growl.info/documentation/developer/gnotify.php)
parser = OptionParser()
parser.add_option("-m", "--message", 
                dest="message", 
                help="Message body/description. Leave blank if inputting from stdin. (Required)", 
                metavar="TEXT")
parser.add_option("-H", "--host", 
                dest="host", 
                help="Hostname or ip (Required)", 
                metavar="TEXT")      
parser.add_option("-p", "--password", 
                dest="password", 
                help="Password (Required)", 
                metavar="TEXT")                
parser.add_option("-t", "--title", 
                dest="title", 
                help="Message title (Default: Alert)", 
                metavar="TEXT",
                default="Alert")
parser.add_option("-n", "--name", 
                dest="name", 
                help="Application name (Default: pygNotify)", 
                metavar="TEXT",
                default="pygNotify")
parser.add_option("-s", "--sticky", 
                dest="sticky", 
                help="Leave the notifcation on screen", 
                action="store_true", 
                default=False, )
parser.add_option("-P", "--priority", 
                dest="priority", 
                type="int", 
                help="Priority, 1-5 (Default: 1)", 
                metavar="NUM", 
                default=1)
parser.add_option("-N", "--notification", 
                dest="notification", 
                help="Notification name (Default: Alert)", 
                metavar="TEXT",
                default="Alert")
parser.add_option("-r", "--register",
                dest="register",
                help="Register application with Growl",
                action="store_true",
                default=False)

(options, args) = parser.parse_args()

if not options.host:
        parser.error("You must specify a hostname")
        
if not options.password:
        parser.error("You must specify a password")

if not options.message:
        options.message = ''
        lines = sys.stdin.readlines()
        for line in lines:
                options.message += line
        options.message = options.message.strip()
        
        # we won't ever get here, since an empty readline will cause pygNotify to wait for input
        if not options.message:
                parser.error("You must input a message either with -m or from stdin.")

addr = (options.host, GROWL_UDP_PORT)
s = socket(AF_INET,SOCK_DGRAM)

if options.register:
        p = GrowlRegistrationPacket(application=options.name, password=options.password)
        p.addNotification(options.notification, enabled=True)
else:
        p = GrowlNotificationPacket(application=options.name,
            notification=options.notification, title=options.title,
            description=options.message, priority=options.priority,
            sticky=options.sticky, password=options.password)
            
s.sendto(p.payload(),addr)
s.close()
